<?php
/**
* Giao diện quản lý admin
*/
class Admin_IndexController extends Zend_Controller_Action {
	public function init() {
		//Load cac model can thiet
		Zend_Loader::loadClass('AuthModel');

	}
		//Nó cũng load trước các hàm có tiền tố pre
	function preDispatch() {
		//Kiem tra dang nhap, neu khong phai o trang login
		if ($this->_request->getActionName() != 'login') {
			$auth = new AuthModel();
			if (!$auth->hasIdentity()) {
				$this->_redirect('/admin/index/login');
			} else {
				//nhay den trang default, neu tk ko phai admin
			}
		}
		/* đăng nhập thành công */
		//      Khoi tao layout
		$layoutPath = TEMPLATE_PATH ."/admin"  ;
		$option = array (	'layout' 		=> 'index', 
	              	        'layoutPath' 	=> $layoutPath );
		Zend_Layout::startMvc ( $option );
	}

	public function indexAction() {
		$this->view->headTitle()->set('Trang Quản Lý - TTGS');

		Zend_Loader::loadClass('IndexModel');
		//      Them cac file js va css can thiet
		$this->view->headScript()->appendFile('/js/DataTables-1.9.0/js/jquery.dataTables.min.js');
        $this->view->headScript()->appendFile("/js/DataTables-1.9.0/media/js/TableTools.js");
        $this->view->headScript()->appendFile("/js/DataTables-1.9.0/media/js/ZeroClipboard.js");
        $this->view->headLink()->appendStylesheet("/js/DataTables-1.9.0/css/demo_table.css");
        $this->view->headLink()->appendStylesheet("/js/DataTables-1.9.0/media/css/TableTools.css");
		$this->view->headLink()->appendStylesheet("/js/DataTables-1.9.0/css/demo_table_jui.css");
		
		if (isset($_POST['type'])) {
			$type = array('type' => $_POST['type']);
		}
		else {
			$type = array('type' => 1);
		}

		$model = new IndexModel();
		$this->view->type = $type['type'];
		if(isset($_GET['ajax'])){
			$params = array('type'=>$_GET['type']);
			echo $model->getDataForDT($params);
			exit();
		}

	}

	/*XEM CHI TIET LOP HOC */
	public function infoClassAction() { 
		$params = $this->_request->getParams();
		if (isset($params['maLop']) && isset($params['maGV'])) {
			
		}
	}

	public function loginAction() {
		//check authenticated
		$auth = new AuthModel();
		if ($auth->hasIdentity())
			$this->_redirect('/admin/index');


		Zend_Loader::loadClass('UserModel');

		$this->view->headTitle()->set('Đăng nhập');

		//      set layout
    	$layoutPath = TEMPLATE_PATH ."/admin"  ;
		$option = array (	'layout' 		=> 'login', 
              	        'layoutPath' 	=> $layoutPath );
		Zend_Layout::startMvc ( $option );
		
		if ($this->_request->isPost()) { //Kiem tra co post khong
			$username = $this->_request->getParam('username');
			$password = $this->_request->getParam('password');

			$model = new UserModel();
			$userPowers = $model->checkUserLogin($username, $password); //Kiem tra tai khoan, tra ve quyen han cua tai khoan 

			if ($userPowers == 1) {
				//xac thuc quyen admin
				$auth->setIdentity($username);
				$this->_redirect('/admin/index');
			} else if ($userPowers == false){
				$message = 'Tài khoản hoặc mật khẩu không đúng';
				$this->view->warning_messages = $message;
			} 
			else
			{
				$message = "$username không có quyền hạn này";
				$this->view->error_messages = $message;
			}
		}

	}

	public function logoutAction() {
		$auth = new AuthModel();
		$auth->clearIdentity();
		$this->_redirect('admin/index/login');
	}
}
?>